﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Diagnostics;
using Memcached.ClientLibrary;

namespace MemCached
{
    public class CMemCached
    {

        //套接字连接池
        SockIOPool pool;
        //缓冲池客户端对象 
        MemcachedClient mc = null;
        //构造函数 
        public CMemCached()
        {
            char[] separator = { ',' };
            string[] serverlist = System.Configuration.ConfigurationManager.AppSettings["Memcached.ServerList"].Split(separator);

            // initialize the pool for memcache servers
            try
            {
                pool = SockIOPool.GetInstance("MyPool");
                pool.SetServers(serverlist);

                pool.InitConnections = 3;
                pool.MinConnections = 3;
                pool.MaxConnections = 50;

                pool.SocketConnectTimeout = 1000;
                pool.SocketTimeout = 3000;

                pool.MaintenanceSleep = 30;
                pool.Failover = true;

                pool.Nagle = false;
                pool.Initialize();
                mc = new MemcachedClient();
                mc.PoolName = "MyPool";

            }
            catch (Exception err)
            {
                //这里就可以用Log4Net记录Error啦！
            }
        }
        /// <summary>
        /// 存储一个对象 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool SetObject(string key, Object value)
        {

            try
            {
                mc.Set(key, value);
                return true;

            }
            catch (Exception err)
            {
                //这里就可以用Log4Net记录Error啦！
                return false;
            }



        }
        /// <summary>
        /// 获取一个对象
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public Object GetObject(string key)
        {
            return mc.Get(key);
        }
        /// <summary>
        /// 判断对象是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool KeyExist(string key)
        {
            return mc.KeyExists(key);
        }
        /// <summary>
        /// 删除一个对象
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool DelObject(string key)
        {
            return mc.Delete(key);
        }
        /// <summary>
        /// 替换已存在的对象
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool ReplaceObject(string key, Object value)
        {
            return mc.Replace(key, value);
        }
        /// <summary>
        /// 清空缓存
        /// </summary>
        /// <returns></returns>
        public bool FlushAll()
        {
            return mc.FlushAll();

        }
        /// <summary>
        /// 关闭缓冲池
        /// </summary>
        /// <returns></returns>
        public void ClosePool()
        {
            pool.Shutdown();
        }

    }
}
